﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace iMusik.Mp3ZingOnline
{
    public class ZingTop
    {
        private ZingTopObject _TopVN; // viet nam
        private ZingTopObject _TopUS; // au - my 
        private ZingTopObject _TopKR; // han quoc

        public ZingTopObject TopVN
        {
            get { return _TopVN; }
            private set { _TopVN = value; }
        }
        public ZingTopObject TopUS
        {
            get { return _TopUS; }
            private set { _TopUS = value; }
        }
        public ZingTopObject TopKR
        {
            get { return _TopKR; }
            private set { _TopKR = value; }
        }

        private string _TopLink;
        private string _LinkTopVN;
        private string _LinkTopUS;
        private string _LinkTopKR;
        private bool _IsSetVN;
        private bool _IsSetUS;
        private bool _IsSetKR;

        public ZingTop(string TopUrl)
        {
            _TopLink = TopUrl;
            _LinkTopVN = _LinkTopUS = _LinkTopKR = "";
            _IsSetVN = _IsSetUS = _IsSetKR = false;
        }

        public async Task GetLink()
        {
            try
            {
                string source = await Mp3Zing.DownloadString(new Uri(_TopLink));
                int i = 0;
                while (i < source.Length)
                {
                    if (source[i] == 34 || source[i] == '\n')
                    {
                        source = source.Remove(i, 1);
                        i--;
                    }
                    i++;
                }

                string pattern = @"(?<=class=select fn-select-link).*?(class=select fn-select-link)";
                Regex regex = new Regex(pattern);
                MatchCollection mc = regex.Matches(source);
                source = mc[0].Value;

                pattern = @"(?=bang-xep-hang).*?(html)";
                Regex regex1 = new Regex(pattern);
                MatchCollection mc1 = regex1.Matches(source);

                string headLink = "http://m.mp3.zing.vn/";
                _LinkTopVN = headLink + mc1[0].Value;
                _LinkTopUS = headLink + mc1[1].Value;
                _LinkTopKR = headLink + mc1[2].Value;
                return;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetTopVN()
        {
            if (_IsSetVN)
                return;
            try
            {
                if (_LinkTopVN == "")
                {
                    await GetLink();
                }
                _TopVN = new ZingTopObject(_LinkTopVN);
                await _TopVN.SetAllInformation();
                _IsSetVN = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetTopUS()
        {
            if (_IsSetUS)
                return;
            try
            {
                if (_LinkTopUS == "")
                {
                    await GetLink();
                }
                _TopUS = new ZingTopObject(_LinkTopUS);
                await _TopUS.SetAllInformation();
                _IsSetUS = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }

        }

        public async Task SetTopKR()
        {
            if (_IsSetKR)
                return;
            try
            {
                if (_LinkTopKR == "")
                {
                    await GetLink();
                }
                _TopKR = new ZingTopObject(_LinkTopKR);
                await _TopKR.SetAllInformation();
                _IsSetKR = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
    }
}
